#对两个新的样本
# X1=(年龄：>40,收入：高，是否为学生：是，信誉：优)
# X2=(年龄：31~40,收入：低，是否为学生：否，信誉：中) 进行分类

import pandas as pd
import numpy as np
from sklearn.preprocessing import OneHotEncoder
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import KBinsDiscretizer

enc = OneHotEncoder()
df = pd.read_excel("D:\\A_TXT文件\\sheet.xlsx", sheet_name="Sheet1")
df_encoded= pd.get_dummies(df, columns=['age', 'income', 'students', 'credit']) 
#分离特征和标签，特征集为X
X = df_encoded.drop('buy', axis=1)
y = df_encoded['buy']
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, y, test_size=0.2, random_state=666)

mnb = MultinomialNB(alpha=1.0e-10, fit_prior=True, class_prior=None)
mnb.fit(Xtrain, Ytrain)
# 创建新样本数据的数据框，并进行与训练数据相同的预处理
X12 = pd.DataFrame([['>40', '高', '是', '优'], ['31~40', '低', '否', '中']], columns=['age', 'income', 'students', 'credit'])
X2 = pd.get_dummies(X12)
X12_encoded = X2.reindex(columns=Xtrain.columns, fill_value=0)
X_predict = mnb.predict(X12_encoded)
print("新样本的预测结果：", X_predict)